
<?php

use Core\Widget\WidgetFactory;
use Model\BlockInstance;

$module = \Core\Session\ApplicationSessionHelper::getCurrentModule();

if($module != 'admin')
    $isEdit = false;
else $isEdit = true;

$divId = $this->divId;
$allBlocks = $this->allBlocks;

if(!$isEdit){

    $myBlock = null;
    foreach ($allBlocks as $blockItem) {
        if($blockItem->div_id == $divId){

            $myBlock = $blockItem;
            break;
        }
    }
    if(!$myBlock instanceof BlockInstance) return 'Không load được block';

    $blockClassItem = $myBlock->getBlockClass();
    $widget_Factory = new WidgetFactory();
    $widget_Abstract = $widget_Factory->getWidget($blockClassItem->program_code);
    echo $widget_Abstract->render($myBlock);

}else{
    $myBlock = null;
    foreach ($allBlocks as $blockItem) {
        if($blockItem->div_id == $divId){

            $myBlock = $blockItem;
            break;
        }
    }
    $content = '';

    if($myBlock){
        if(!$myBlock instanceof BlockInstance) return 'Không load được block';
        $blockClassItem = $myBlock->getBlockClass();
        $widget_Factory = new WidgetFactory();
        $widget_Abstract = $widget_Factory->getWidget($blockClassItem->program_code);
        $content = $widget_Abstract->render($myBlock);
    }

    echo '
       <div class="block-container" id="block-container_'.$divId.'">
        <div style="padding:0px 0px 0px 0px" id="'.$divId.'" class = "block-view" >'.$content.'</div>
        <div class="action-panel" id = "actionPanel'.$divId.'">
            <div style="float:left; width:100px">
                 <a id = "lnkAddBlock'.$divId.'"  href="javascript:openBlockSelector(\''.$divId.'\')" ><i class="fa fa-lg fa-plus"></i>&nbsp;<span>Thêm (Đổi)</span></a>
                 &nbsp;
            </div>
            <div style="float:left; width:70px;display:none">
                 <a id = "lnkEditBlock'.$divId.'"  href="javascript:editBlock(\''.$divId.'\')" ><i class="fa fa-lg fa-edit"></i>&nbsp;<span>Sửa</span></a>
                 &nbsp;
            </div>
            <div style="float:left; width:50px;display:none">
                 <a  id = "lnkRemoveBlock'.$divId.'" href="javascript:removeBlock(\''.$divId.'\');" ><i class="fa fa-lg fa-times"></i>&nbsp;<span>Xóa</span></a>
                   &nbsp;
            </div>&nbsp;&nbsp;

            <div style="clear:both">
            </div>
        </div>
        <div class="edit-panel" id="editPanel'.$divId.'" >
            <div class="edit-editor" id="editor'.$divId.'"></div>
            <div id="control'.$divId.'" class="edit-control">
            </div>
        </div>
       </div>';
}

?>

